# 获取验证码： http://localhost/index.php?m=Home&c=User&a=verify
# 登录 ： http://localhost/index.php?m=Home&c=User&a=do_login
# 导包
import unittest
import requests

# 创建测试类
class testcase_req_login(unittest.TestCase):
    # 测试类实例化开始方法,定义验证码接口setUp
    def setUp(self) :
        # 实例化session
        self.session = requests.session()
        self.url_verify = "http://172.16.18.128/index.php?m=Home&c=User&a=verify"
        self.url_login = "http://172.16.18.128/index.php?m=Home&c=User&a=do_login"

    # 测试类结束方法teardown
    def tearDown(self) :
       self.session.close()

# 定义测试接口

# test01登录成功
    def test01_loginSuccess(self):
        # 获取验证码
        response = self.session.get(url=self.url_verify)

        # 验证登录方法
        login_data = {
            "username": "15988888888",
            "password": "yww123456",
            "verify_code": "8888"
        }
        response = self.session.post(url=self.url_login,data=login_data)
        #测试用例
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, response.json().get("status"))
        self.assertIn("登陆成功", response.json().get("msg"))

# test02账号不存在
    def test_02login_user_not(self):
        # 获取验证码
        response = self.session.get(url=self.url_verify)

        # 验证登录方法
        login_data = {
            "username": "15888888888",
            "password": "yww123456",
            "verify_code": "8888"
        }
        response = self.session.post(url=self.url_login, data=login_data)
        # 测试用例
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(-1, response.json().get("status"))
        self.assertIn("账号不存在", response.json().get("msg"))

# test03密码错误
    def test_03login_password(self):
        # 获取验证码
        response = self.session.get(url=self.url_verify)

        # 验证登录方法
        login_data = {
            "username": "15988888888",
            "password": "123456",
            "verify_code": "8888"
        }
        response = self.session.post(url=self.url_login, data=login_data)
        # 测试用例
        print(response.json())
        # self.
        self.assertEqual(200, response.status_code)
        self.assertEqual(-4, response.json().get("status"))
        self.assertIn("密码错误", response.json().get("msg"))